package cn.edu.neusoft.wuguangsheng.flowerimpression.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.AnswerActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.OtherUserInfoActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.UserAnswerActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.AnswerAskBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.DeleteMyAnswerModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.service.Service;

public class AnswerAskViewAdapter extends BaseAdapter<AnswerAskBean> {
    public AnswerAskViewAdapter(Context context, List<AnswerAskBean> items, int layoutResourse) {
        super(context, items, layoutResourse);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView= LayoutInflater.from(context).inflate(layoutResourse,parent,false);
        AnswerAskViewHolder viewHolder = new AnswerAskViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        AnswerAskViewHolder viewHolder=(AnswerAskViewHolder) holder;
        AnswerAskBean answerAskBean=items.get(position);
        //final String img = answerAskBean.getUser_photo();
        final String username = answerAskBean.getUser_nickname();
        final String asktime = answerAskBean.getAsk_time();
        final String askcontent = answerAskBean.getAsk_content();
        final String answertime = answerAskBean.getAnswer_time();
        final String answercontent = answerAskBean.getAnswer_content();
        final String sex = answerAskBean.getUser_sex();
        final String ask_id = answerAskBean.getAsk_id();
        final String answer_id = answerAskBean.getAnswer_id();
        final String userid = answerAskBean.getUser_id();

        viewHolder.answer_ask_username.setText(username);
        viewHolder.answer_ask_asktime.setText(asktime);
        viewHolder.answer_ask_askcontent.setText(askcontent);
        viewHolder.answer_ask_answertime.setText(answertime);
        viewHolder.answer_ask_answercontent.setText(answercontent);


        if (answerAskBean.getUser_photo().equals("1") || answerAskBean.getUser_photo().equals("1.jpg")){
            if (sex.equals("1")){
                viewHolder.answer_ask_img.setImageResource(R.mipmap.nanshi);
            }
            else{
                viewHolder.answer_ask_img.setImageResource(R.mipmap.nvshi);
            }
        }
        else{
            String img = Service.BASE_URL+"flower_impression/public/uploads/"+answerAskBean.getUser_photo();
            Picasso.with(context).load(img).into(viewHolder.answer_ask_img);
        }
        viewHolder.answer_ask_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intentu = new Intent(context, OtherUserInfoActivity.class);
                intentu.putExtra("userid",userid);
                context.startActivity(intentu);
            }
        });

        viewHolder.answer_ask_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DeleteMyAnswerModel deleteMyAnswerModel = new DeleteMyAnswerModel();
                deleteMyAnswerModel.deleteMyAnswer(answer_id, new TListener<SuccessBean>() {
                    @Override
                    public void onResponse(SuccessBean successBean) {
                        if (successBean.getSuccess().equals("1")){
                            if(UserAnswerActivity.class.isInstance(context)){

                                UserAnswerActivity activity = (UserAnswerActivity)context;
                                activity.finish();}
                            Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show();
                        }
                        else {
                            Toast.makeText(context,"删除失败",Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFail(String msg) {
                        Toast.makeText(context,"删除失败",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, AnswerActivity.class);
                intent.putExtra("ask_id",ask_id);
                context.startActivity(intent);
            }
        });
    }
}
